package com.zkthink.ceres.applet.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.authority.domain.entity.AppletMember;
import com.zkthink.ceres.applet.authority.mapper.AppletMemberMapper;
import com.zkthink.ceres.applet.ceresStore.domain.entity.CeresStore;
import com.zkthink.ceres.applet.ceresStore.mapper.CeresStoreMapper;
import com.zkthink.ceres.applet.commission.mapper.AppletCommissionMapper;
import com.zkthink.ceres.applet.commission.service.AppletCommissionService;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.contract.domain.entity.AppletContract;
import com.zkthink.ceres.applet.contract.mapper.AppletContractMapper;
import com.zkthink.ceres.applet.feedback.domain.vo.AppletFeedbackVo;
import com.zkthink.ceres.applet.order.domain.entity.AppletOrderAssess;
import com.zkthink.ceres.applet.order.domain.entity.AppletServerOrder;
import com.zkthink.ceres.applet.order.domain.vo.*;
import com.zkthink.ceres.applet.order.mapper.AppletOrderAssessMapper;
import com.zkthink.ceres.applet.order.mapper.AppletServerOrderMapper;
import com.zkthink.ceres.applet.order.service.AppletServerOrderService;
import com.zkthink.ceres.applet.pcVehicles.domain.entity.PcVehicles;
import com.zkthink.ceres.applet.pcVehicles.mapper.PcVehiclesMapper;
import com.zkthink.ceres.applet.product.domain.entity.AppletProduct;
import com.zkthink.ceres.applet.product.mapper.AppletProductMapper;
import com.zkthink.ceres.applet.product.mapper.AppletStoreProductMapper;
import com.zkthink.ceres.applet.product.mapper.AppletVehiclesProductMapper;
import com.zkthink.ceres.applet.server.domain.entity.AppletServer;
import com.zkthink.ceres.applet.server.mapper.AppletServerMapper;
import com.zkthink.ceres.common.utils.QcodeUtil;
import com.zkthink.ceres.file.properties.FileServerProperties;
import com.zkthink.ceres.pay.service.PayService;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.clh.order.dto.MyqianbaoDto;
import com.zkthink.clh.order.vo.MyqianbaoVo;
import com.zkthink.clh.order.vo.MyqianbaodetailsVo;
import com.zkthink.utils.StrPool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.order.mapper.AppletProductOrderMapper;
import com.zkthink.ceres.applet.order.domain.entity.AppletProductOrder;
import com.zkthink.ceres.applet.order.service.AppletProductOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;
import static com.zkthink.ceres.applet.order.service.impl.AppletServerOrderServiceImpl.wxPay2;
import static com.zkthink.utils.DateUtils.DEFAULT_MONTH_FORMAT_SLASH;


/**
 * @Author cmz
 * @Date 2024-04-30 9:33
 */
@Service
public class AppletProductOrderServiceImpl extends ServiceImpl<AppletProductOrderMapper, AppletProductOrder> implements AppletProductOrderService {

    @Resource
    private FileServerProperties fileProperties;

    @Resource
    private AppletProductOrderMapper appletProductOrderMapper;

    @Resource
    private AppletServerOrderMapper appletServerOrderMapper;

    @Resource
    private AppletContractMapper appletContractMapper;
    @Resource
    private CeresStoreMapper ceresStoreMapper;

    @Autowired
    private PcVehiclesMapper pcVehiclesMapper;

    @Resource
    private AppletServerOrderService appletServerOrderService;

    @Resource
    private PayService payService;

    @Resource
    private AppletMemberMapper appletMemberMapper;
    @Resource
    private AppletProductMapper appletProductMapper;
    @Resource
    private AppletServerMapper appletServerMapper;
    @Resource
    private AppletCommissionService appletCommissionService;
    @Resource
    private AppletCommissionMapper appletCommissionMapper;
    @Resource
    private AppletOrderAssessMapper appletOrderAssessMapper;

    @Override
    public R isFirstProductOrder() {
        Long mid = getMid();
        QueryWrapper<AppletProductOrder> appletProductOrderQueryWrapper = new QueryWrapper<>();
        appletProductOrderQueryWrapper.eq("mid", mid);
        Integer count = appletProductOrderMapper.selectCount(appletProductOrderQueryWrapper);
        AppletContract appletContract = null;
        //count=0;
        if (count == 0) {
            //第一次下单
            //查询电子合约，响应电子合约
            appletContract = appletContractMapper.selectOne(new QueryWrapper<>());
            return R.success(appletContract);
        }
        return R.success(appletContract);
    }

    @Override
    public R placeServerOrder(PlaceProductOrderVo placeProductOrderVol) {
        AppletProductOrder appletProductOrder = new AppletProductOrder();
        Long mid = getMid();
        appletProductOrder.setMid(mid);
        appletProductOrder.setProductId(placeProductOrderVol.getProductId());
        appletProductOrder.setDiseasesSuffered(placeProductOrderVol.getDiseasesSuffered());
        appletProductOrder.setIllnessTime(placeProductOrderVol.getIllnessTime());
        appletProductOrder.setPayChannel(placeProductOrderVol.getPayChannel());
        appletProductOrder.setPayStatus(1);//初始化设置支付状态为未支付
        appletProductOrder.setProductStatus(1);//初始化设置订单状态为待选择
        appletProductOrder.setCosts(placeProductOrderVol.getCosts());//设置产品价格
        appletProductOrder.setOrderCode(UUID.randomUUID().toString());
        int insert = appletProductOrderMapper.insert(appletProductOrder);
        return insert > 0 ? R.success() : R.fail("添加订单失败");
    }

    @Override
    public R setDeliveryMethod(SetDeliveryMethod setDeliveryMethod) {
        //获取到已有订单信息
        QueryWrapper<AppletProductOrder> appletProductOrderQueryWrapper = new QueryWrapper<>();
        appletProductOrderQueryWrapper.eq("id", setDeliveryMethod.getId());
        AppletProductOrder order = appletProductOrderMapper.selectOne(appletProductOrderQueryWrapper);
        if (order == null) {
            return R.fail("没有找到订单信息");
        }
        AppletProductOrder appletProductOrder = new AppletProductOrder();
        appletProductOrder.setId(setDeliveryMethod.getId());
        appletProductOrder.setDeliveryMethod(setDeliveryMethod.getDeliveryMethod());
        if (setDeliveryMethod.getDeliveryMethod() == 2) {
            //门店取货
            appletProductOrder.setStoreId(setDeliveryMethod.getStoreId());
        } else if (setDeliveryMethod.getDeliveryMethod() == 1) {
            //车辆取货
            appletProductOrder.setVehiclesId(setDeliveryMethod.getStoreId());

        } else if (setDeliveryMethod.getDeliveryMethod() == 0) {
            //快递取货
            appletProductOrder.setStoreId(setDeliveryMethod.getStoreId());
            appletProductOrder.setAddressId(setDeliveryMethod.getAddressId());

        } else {
            return R.fail("取货方式填写错误");
        }
        if (order.getProductStatus() != 1) {
            return R.fail("当前订单的状态未处于《待选择》，设置失败!!!");
        }
        appletProductOrder.setProductStatus(2);
        int i = appletProductOrderMapper.updateById(appletProductOrder);
        return i > 0 ? R.success() : R.fail("设置取货方式失败!!!");
    }

    @Override
    public R<Object> payOrder(Long mid, Long orderId, String clientIP) {
        AppletProductOrder order = appletProductOrderMapper.selectOne(new QueryWrapper<AppletProductOrder>().eq("id", orderId));
        if (order.getPayStatus() != 1) {
            return R.fail("订单未处于待支付状态!!!");
        }
        if (order.getCosts() == null) {
            return R.fail("未选择收货方式，无法计算费用!!!");
        }

        //生成核销用二维码
        order.setCodeUrl(qRCodeUrl(String.valueOf(order.getId())));

        order.setPayTime(new Date());
        AppletMember member = appletMemberMapper.selectOne(new QueryWrapper<AppletMember>().eq("id", mid));
        Integer payChannel = order.getPayChannel();
        if (payChannel == 1) {
            //微信支付
            WxPayAppOrderResult result = wxPay2(order.getOrderCode(), order.getCosts(), member.getOpenid(), clientIP, null, payService);
            if (result.getPackageValue() != null) {
                order.setPrepayId(JSON.toJSONString(result));
                order.setPayStatus(2);//修改状态为已支付
                order.setProductStatus(2);//修改订单状态为待收货
//                baseMapper.updateById(order);
                appletProductOrderMapper.updateById(order);
            } else {
                return R.fail("支付失败");
            }
            return R.success(result);
        } else {
            //余额支付
            BigDecimal balance = member.getBalance();//用户的余额
            if (balance.compareTo(order.getCosts()) < 0) {//余额小于费用
                return R.fail("支付失败,余额不足");
            }
            member.setBalance(balance.subtract(order.getCosts()));//余额减去费用
            appletMemberMapper.updateById(member);

            order.setPayStatus(2);//修改状态为已支付
            order.setProductStatus(2);//修改订单状态为待收货
            appletProductOrderMapper.updateById(order);

        }
        return R.success("支付成功");
    }

    @Override
    @Transactional
    public R confirmReceipt(Long orderId) {
        QueryWrapper<AppletProductOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", orderId);
        AppletProductOrder order = appletProductOrderMapper.selectOne(queryWrapper);
        if (order.getProductStatus() != 2) {
            return R.fail("订单状态错误!!!");
        }
        order.setProductStatus(3);
        int i = appletProductOrderMapper.updateById(order);

        //添加提成信息
        appletCommissionService.addCommission(order.getMid(), order.getPaySum());

        if (order.getDeliveryMethod() == 2) {
            //门店取货
            QueryWrapper<CeresStore> ceresStoreQueryWrapper = new QueryWrapper<>();
            ceresStoreQueryWrapper.eq("id", order.getStoreId());

            CeresStore ceresStore = ceresStoreMapper.selectOne(ceresStoreQueryWrapper);
            ceresStore.setOrderQuantity(ceresStore.getOrderQuantity() + 1);//订单数量+1
            ceresStoreMapper.updateById(ceresStore);
        }
        AppletMember appletMember = new AppletMember();
        if (order.getStoreId() != null) {
            CeresStore ceresStore = ceresStoreMapper.selectById(order.getStoreId());
            if (ceresStore != null) {
                appletMember = appletMemberMapper.selectById(ceresStore.getMid());
            }
        } else {
            PcVehicles pcVehicles = pcVehiclesMapper.selectById(order.getVehiclesId());
            if (pcVehicles != null) {
                appletMember = appletMemberMapper.selectById(pcVehicles.getUserId());
            }
        }
        if (appletMember.getId() != null) {
            appletMember.setBalance(appletMember.getBalance().add(order.getPaySum()));
            appletMemberMapper.updateById(appletMember);
        }
        return R.success();
    }

    @Override
    public R cancelPay(OrderStatusVo orderStatusVo) {
        QueryWrapper<AppletProductOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("id", orderStatusVo.getOrderId());

        AppletProductOrder order = appletProductOrderMapper.selectOne(orderQueryWrapper);
        if (order == null) {
            return R.fail("没有找到该订单!!!");
        }
        if (order.getPayStatus() == 2) {
            return R.success("该订单已支付，无法取消!!!");
        }
        if (order.getPayStatus() == 3) {
            return R.success("该订单已取消,不能重复取消!!!");
        }
        order.setPayStatus(3);
        int i = appletProductOrderMapper.updateById(order);
        return i > 0 ? R.success() : R.fail("订单取消失败");
    }

    @Override
    public R findProductOrderList(PageByOrderTypeVo pageByOrderTypeVo) {
        Page<OrderListVo> page = new Page<>(pageByOrderTypeVo.getPageNum(), pageByOrderTypeVo.getPageSize());
        Page<OrderListVo> pageList;
        Long mid = getMid();
        if (pageByOrderTypeVo.getOrderType() == 1) {
            pageList = appletProductOrderMapper.selectProductOrderListByPage(page, mid);
            return R.success(pageList);
        } else if (pageByOrderTypeVo.getOrderType() == 0) {
            pageList = appletServerOrderMapper.selectServerOrderListByPage(page, mid);
            return R.success(pageList);
        }
        return R.fail("参数错误");

    }

    @Override
    public AppletProductOrder selectByOrderCode(String orderCode) {
        QueryWrapper<AppletProductOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_code", orderCode);
        AppletProductOrder appletProductOrder = appletProductOrderMapper.selectOne(queryWrapper);
        return appletProductOrder;
    }

    @Override
    public R refreshOrderCodeUrl(String orderCode) {
        String codeUrl = qRCodeUrl(orderCode);
        AppletServerOrder appletServerOrder = new AppletServerOrder();
        appletServerOrder.setOrderCode(orderCode);
        appletServerOrder.setCodeUrl(codeUrl);
        UpdateWrapper<AppletServerOrder> serverOrderUpdateWrapper = new UpdateWrapper<>();
        serverOrderUpdateWrapper.eq("order_code", orderCode);
        int count = appletServerOrderMapper.update(appletServerOrder, serverOrderUpdateWrapper);
        if (count == 0) {
            AppletProductOrder appletProductOrder = new AppletProductOrder();
            appletProductOrder.setOrderCode(orderCode);
            appletProductOrder.setCodeUrl(codeUrl);
            UpdateWrapper<AppletProductOrder> productOrderUpdateWrapper = new UpdateWrapper<>();
            productOrderUpdateWrapper.eq("order_code", orderCode);
            appletProductOrderMapper.update(appletProductOrder, productOrderUpdateWrapper);
        }
        return R.success(codeUrl);
    }

    @Override
    public R<OrderDetailVo> findOrderDetail(String orderCode) {
        AppletProductOrder productOrder = this.selectByOrderCode(orderCode);
        AppletServerOrder serverOrder = appletServerOrderService.selectByOrderCode(orderCode);
        OrderDetailVo orderDetailVo = new OrderDetailVo();

        if (productOrder != null) {
            //产品订单
            orderDetailVo.setId(productOrder.getId());
            orderDetailVo.setOrderCode(productOrder.getOrderCode());
            orderDetailVo.setPayStatus(productOrder.getPayStatus());
            orderDetailVo.setPayTime(productOrder.getPayTime());
            orderDetailVo.setServerStatus(productOrder.getProductStatus());
            orderDetailVo.setPayNum(productOrder.getPayNum());
            orderDetailVo.setCodeUrl(productOrder.getCodeUrl());
            orderDetailVo.setCosts(productOrder.getCosts());
            orderDetailVo.setPaySum(productOrder.getPaySum());
            orderDetailVo.setCreateTime(productOrder.getCreateTime());
            orderDetailVo.setOrderType(1);
            orderDetailVo.setExpressNo(productOrder.getExpressNo());
            if (productOrder.getStoreId() != null) {
                QueryWrapper<CeresStore> ceresStoreQueryWrapper = new QueryWrapper<>();
                ceresStoreQueryWrapper.eq("id", productOrder.getStoreId().toString());
                CeresStore ceresStore = ceresStoreMapper.selectOne(ceresStoreQueryWrapper);
                orderDetailVo.setStoreName(ceresStore.getStoreName());
            } else {
                QueryWrapper<PcVehicles> pcVehiclesQueryWrapper = new QueryWrapper<>();
                if (productOrder.getVehiclesId() != null) {
                    pcVehiclesQueryWrapper.eq("id", productOrder.getVehiclesId().toString());
                    PcVehicles pcVehicles = pcVehiclesMapper.selectOne(pcVehiclesQueryWrapper);
                    orderDetailVo.setLicensePlate(pcVehicles.getLicensePlate());
                }
            }
            AppletProduct appletProduct = appletProductMapper.selectById(productOrder.getProductId());
            orderDetailVo.setServerName(appletProduct.getProductName());
            orderDetailVo.setServerImg(appletProduct.getProductImg());
            AppletMember appletMember = appletMemberMapper.selectById(productOrder.getMid());
            orderDetailVo.setPhone(appletMember.getPhone());
            orderDetailVo.setNickname(appletMember.getNickname());
            orderDetailVo.setHeadimg(appletMember.getHeadimg());
        } else if (serverOrder != null) {
            orderDetailVo.setId(serverOrder.getId());
            orderDetailVo.setOrderCode(serverOrder.getOrderCode());
            orderDetailVo.setPayStatus(serverOrder.getPayStatus());
            orderDetailVo.setPayTime(serverOrder.getPayTime());
            orderDetailVo.setServerStatus(serverOrder.getServerStatus());
            orderDetailVo.setPayNum(serverOrder.getPayNum());
            orderDetailVo.setCodeUrl(serverOrder.getCodeUrl());
            orderDetailVo.setCosts(serverOrder.getCosts());
            orderDetailVo.setPaySum(serverOrder.getPaySum());
            orderDetailVo.setCreateTime(serverOrder.getCreateTime());
            if (serverOrder.getAdviceTime() != null) {
                orderDetailVo.setAdviceTime(new SimpleDateFormat("yyyy-MM-dd").format(serverOrder.getAdviceTime()));
            }
            orderDetailVo.setOrderType(0);
            if (serverOrder.getStoreId() != null) {
                QueryWrapper<CeresStore> ceresStoreQueryWrapper = new QueryWrapper<>();
                ceresStoreQueryWrapper.eq("id", serverOrder.getStoreId().toString());
                CeresStore ceresStore = ceresStoreMapper.selectOne(ceresStoreQueryWrapper);
                orderDetailVo.setStoreName(ceresStore.getStoreName());
            } else {
                QueryWrapper<PcVehicles> pcVehiclesQueryWrapper = new QueryWrapper<>();
                if (serverOrder.getVehiclesId() != null) {
                    pcVehiclesQueryWrapper.eq("id", serverOrder.getVehiclesId().toString());
                    PcVehicles pcVehicles = pcVehiclesMapper.selectOne(pcVehiclesQueryWrapper);
                    orderDetailVo.setLicensePlate(pcVehicles.getLicensePlate());
                }
            }

            AppletServer appletServer = appletServerMapper.selectById(serverOrder.getServerId());
            orderDetailVo.setServerName(appletServer.getTitle());
            orderDetailVo.setServerImg(appletServer.getImg());
            AppletMember appletMember = appletMemberMapper.selectById(serverOrder.getMid());
            orderDetailVo.setPhone(appletMember.getPhone());
            orderDetailVo.setNickname(appletMember.getNickname());
            orderDetailVo.setHeadimg(appletMember.getHeadimg());
            if (orderDetailVo.getServerStatus() == 2) {
                orderDetailVo.setServerOrderStatus(1);//确认按钮
            } else if (orderDetailVo.getServerStatus() == 3) {
                orderDetailVo.setServerOrderStatus(2);//验证核销按钮
            } else if (orderDetailVo.getServerStatus() == 4) {
                orderDetailVo.setServerOrderStatus(3);//去测评
            }
        } else {
            return R.fail("没有找到订单信息");
        }
        List<AppletOrderAssess> appletOrderAssesses = appletOrderAssessMapper.selectList(new QueryWrapper<AppletOrderAssess>().eq("order_code", orderCode));
        if (appletOrderAssesses.size() > 0 ){
            AppletOrderAssess appletOrderAssess = appletOrderAssesses.get(0);
            BeanUtils.copyProperties(appletOrderAssess,orderDetailVo);
            orderDetailVo.setCreateTime(appletOrderAssess.getCreateTime());
        }
        if (orderDetailVo.getAdviceTime() != null) {
            orderDetailVo.setAdviceTime(orderDetailVo.getAdviceTime().replace(" 00:00:00", ""));
        }
        return R.success(orderDetailVo);
    }

    @Override
    public MyqianbaoVo myQianBao(MyqianbaoDto dto, MyqianbaoVo vo) {
        List<MyqianbaodetailsVo> list = vo.getList();
        //筛选该接口可能是单选项
        if (dto == null || dto.getType() == null || (dto.getType() != 1 && dto.getType() != 2 && dto.getType() != 3 && dto.getType() != 6)) {
            //不是单选项再添加该数据
            //添加当前服务订单和产品订单表中数据的检索
            List<MyqianbaodetailsVo> myqianbaodetailsVoList = appletServerOrderMapper.smyqianbao(dto.getMid());
            list.addAll(myqianbaodetailsVoList);
            List<MyqianbaodetailsVo> myqianbao = appletProductOrderMapper.myqianbao(dto.getMid());
            list.addAll(myqianbao);

        }
        vo.setList(list);
        //今日收入
        Long mid = dto.getMid();
        //AppletMember member = appletMemberMapper.selectById(mid);
        //List<AppletMember> memberList =  appletMemberMapper.selectByReferPhone(member.getPhone());
        //List<Long> mids = memberList.stream().map(a -> a.getId()).collect(Collectors.toList());
        BigDecimal jinreshouyi = new BigDecimal("0.00");
        BigDecimal todayDirectCommission = appletCommissionMapper.selectTodayDirectCommission(mid);
        jinreshouyi = jinreshouyi.add(todayDirectCommission);
        BigDecimal todayInDirectCommission = appletCommissionMapper.selectTodayInDirectCommission(mid);
        jinreshouyi = jinreshouyi.add(todayInDirectCommission);
        vo.setJinreshouyi(vo.getJinreshouyi().add(jinreshouyi));
        //累计收入
        BigDecimal leijishouyi = new BigDecimal("0.00");
        BigDecimal directCommission = appletCommissionMapper.selectDirectCommission(mid);
        leijishouyi = leijishouyi.add(directCommission);
        BigDecimal inDirectCommission = appletCommissionMapper.selectInDirectCommission(mid);
        leijishouyi = leijishouyi.add(inDirectCommission);
        vo.setLeijishouyi(vo.getLeijishouyi().add(leijishouyi));
        vo.setRecommend(appletCommissionMapper.selectDirectCommission(mid).add(appletCommissionMapper.selectInDirectCommission(mid)));
        return vo;
    }

    @Override
    public R serverOrderAssess(ServerOrderAssessVo serverOrderAssessVo) {
        QueryWrapper<AppletServerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_code", serverOrderAssessVo.getOrderCode());
        AppletServerOrder serverOrder = appletServerOrderMapper.selectOne(queryWrapper);
        serverOrder.setBodyStatus(serverOrderAssessVo.getBodyStatus());
        serverOrder.setAdviseServerId(serverOrderAssessVo.getAdviseServerId());
        serverOrder.setAdviceTime(serverOrderAssessVo.getAdviceTime());
        serverOrder.setServerStatus(5);//服务已完成
        int i = appletServerOrderMapper.updateById(serverOrder);
        AppletOrderAssess appletOrderAssess = new AppletOrderAssess();
        appletOrderAssess.setBodyStatus(serverOrderAssessVo.getBodyStatus());
        appletOrderAssess.setAdviseServerId(String.valueOf(serverOrderAssessVo.getAdviseServerId()));
        appletOrderAssess.setAdviceTime(serverOrderAssessVo.getAdviceTime());
        appletOrderAssess.setOrderCode(serverOrder.getOrderCode());
        appletOrderAssess.setCreateUser(String.valueOf(getMid()));
        appletOrderAssess.setCreateTime(new Date());
        appletOrderAssess.setUserId(String.valueOf(serverOrder.getMid()));
        //添加用户信息
        AppletMember appletMember = appletMemberMapper.selectById(serverOrder.getMid());
        if (appletMember  != null){
            appletOrderAssess.setUserName(appletMember.getNickname());
            appletOrderAssess.setUserPhone(appletMember.getPhone());
            appletOrderAssess.setUserHeadImg(appletMember.getHeadimg());
        }
        appletOrderAssess.setUserId(String.valueOf(serverOrder.getMid()));
        //添加推荐服务名称
        AppletServer appletServer = appletServerMapper.selectById(serverOrderAssessVo.getAdviseServerId());
        if (appletServer != null){
            appletOrderAssess.setAdviseServer(appletServer.getTitle());
            appletOrderAssess.setAdviseServerPrice(appletServer.getStorePrice());
            appletOrderAssess.setAdviseServerImg(appletServer.getImg());
        }else{
            AppletProduct appletProduct = appletProductMapper.selectById(serverOrderAssessVo.getAdviseServerId());
            if (appletProduct != null){
                appletOrderAssess.setAdviseServer(appletProduct.getProductName());
                appletOrderAssess.setAdviseServerPrice(appletProduct.getStorePrice());
                appletOrderAssess.setAdviseServerImg(appletProduct.getProductImg());
            }
        }
        //添加当前服务名称
        AppletServer server = appletServerMapper.selectById(serverOrder.getServerId());
        if (server != null){
            appletOrderAssess.setServerName(server.getTitle());
        }else{
            AppletProduct appletProduct = appletProductMapper.selectById(serverOrder.getServerId());
            if (appletProduct != null){
                appletOrderAssess.setServerName(appletProduct.getProductName());
            }
        }
        //添加店铺、理疗车信息
        if (serverOrder.getStoreId() != null){
            Store byId = ceresStoreMapper.findById(serverOrder.getStoreId());
            if (byId != null){
                appletOrderAssess.setStoreName(byId.getStoreName());
            }
        }else{
            PcVehicles pcVehicles = pcVehiclesMapper.selectById(serverOrder.getStoreId());
            if (pcVehicles != null){
                appletOrderAssess.setStoreName(pcVehicles.getLicensePlate());
            }
        }
        List<AppletOrderAssess> appletOrderAssesses = appletOrderAssessMapper.selectList(new QueryWrapper<AppletOrderAssess>().eq("order_code", serverOrderAssessVo.getOrderCode()));
        if (appletOrderAssesses.size() == 0){
            appletOrderAssessMapper.insert(appletOrderAssess);
        }
        return i > 0 ? R.success() : R.fail("修改成功");
    }

    @Override
    public R selectAssess(PageDate pageDate) {
        Page<AppletOrderAssess> page = new Page<>(pageDate.getPageNum(), pageDate.getPageSize());
        Page<AppletOrderAssess> appletOrderAssessPage = appletOrderAssessMapper.selectPage(page, new QueryWrapper<AppletOrderAssess>().eq("create_user", String.valueOf(getMid())).or().eq("user_id", String.valueOf(getMid())).orderByDesc("create_time"));
        return R.success(appletOrderAssessPage);
    }

    //生成二维码图片地址
    public String qRCodeUrl(String content) {
        String fileName = UUID.randomUUID().toString() + StrPool.DOT + "png";
        //日期文件夹
        String relativePath = Paths.get(LocalDate.now().format(DateTimeFormatter.ofPattern(DEFAULT_MONTH_FORMAT_SLASH))).toString();
        // web服务器存放的绝对路径
        String absolutePath = Paths.get(fileProperties.getStoragePath(), relativePath).toString();
        QcodeUtil.getQRCode(content, Paths.get(absolutePath, fileName).toString());
        String url = new StringBuilder(fileProperties.getUriPrefix())
                .append(relativePath)
                .append(StrPool.SLASH)
                .append(fileName)
                .toString();

        //替换掉windows环境的\路径
        url = StrUtil.replace(url, "\\\\", StrPool.SLASH);
        url = StrUtil.replace(url, "\\", StrPool.SLASH);
        return url;
    }


}
